//
//  CheckHelper.m
//  FreshMember
//
//  Created by qyc on 2019/4/19.
//  Copyright © 2019年 qyc. All rights reserved.
//

#import "CheckHelper.h"

@implementation CheckHelper
/**
 *  验证手机号码格式
 *
 *  @param telNumber 手机号
 *
 *  @return 返回是否正确
 */
+ (BOOL)checkTelNumber:(NSString *) telNumber {
    NSString *pattern = @"^1+[345789]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@" , pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    
    return isMatch;
}

/**
 *  判断邮箱格式是否正确
 *
 *  @param email 判断的邮箱
 *
 *  @return 返回是否正确
 */
+ (BOOL)validateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL isMatch = [emailTest evaluateWithObject:email];
    
    return isMatch;
}

/**
 *  判断密码格式
 *
 *  @param passWord 需要判断的密码
 *
 *  @return 返回是否正确
 */
+ (BOOL)validatePassword:(NSString *)passWord {
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    BOOL isMatch = [passWordPredicate evaluateWithObject:passWord];
    return isMatch;
}

/**
 *  判断昵称格式
 *
 *  @param nickname 需要判断的昵称
 *
 *  @return 返回是否正确
 */
+ (BOOL)validateNickname:(NSString *)nickname {
    NSString *nicknameRegex = @"([\u4e00-\u9fa5A-Za-z0-9]{2,10})(&middot;[\u4e00-\u9fa5]{2,5})*";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    BOOL isMatch = [passWordPredicate evaluateWithObject:nickname];
    return isMatch;
}

/**
 *  判断字符串是否空
 *
 *  @param string 需要判断的字符串
 *
 *  @return 返回是否正确
 */
+ (BOOL)isStringEqualNil:(NSString *)string {
    
    if (string==nil||[string isEqualToString:@""]) {
        return NO;
        
    }
    else
    {
        return YES;
        
    }
    
}

/**
 *  判断标点符号
 *
 *  @param passWord 需要判断的字符串
 *
 *  @return 返回是否正确
 */
+ (BOOL)validatePunctuation:(NSString *)passWord {
    NSString *passWordRegex = @"^[\u4E00-\u9FA5A-Za-z0-9_，。、%·！*；’‘（）·~《》？——+：“”、]+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    BOOL isMatch = [passWordPredicate evaluateWithObject:passWord];
    return isMatch;
}

/**
 判断是否输入了emoji 表情
 */
+ (BOOL)stringContainsEmoji:(NSString *)string {
    
    __block BOOL isEomji = NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        const unichar hs = [substring characterAtIndex:0];
        // surrogate pair
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    isEomji = YES;
                }
            }
        } else {
            // non surrogate
            if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                isEomji = YES;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                isEomji = YES;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                isEomji = YES;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                isEomji = YES;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
                isEomji = YES;
            }
            if (!isEomji && substring.length > 1) {
                const unichar ls = [substring characterAtIndex:1];
                if (ls == 0x20e3) {
                    isEomji = YES;
                }
            }
        }
    }];
    return isEomji;
}

/**
 判断是否只有数字
 */
+ (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

@end
